package com.ttai.presenter.activity;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.ttai.model.dao.DBHelper;
import com.ttai.model.net.AppNameBean;
import com.ttai.model.net.ArrayInfo;
import com.ttai.presenter.base.BaseArrayPresenter;
import com.ttai.ui.activity.LoginId;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;

public class AppNamePresenter extends BaseArrayPresenter {
    LoginId loginId;
    private String productName;
    private String id;
    private String appName;
    private ArrayList<AppNameBean> list = new ArrayList<>();

    public AppNamePresenter(LoginId loginId) {
        this.loginId = loginId;
    }

    @Override
    protected void showError(String message) {
        Toast.makeText(loginId, message, Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void parseArray(JsonArray json) {
        Log.i("12", "parseArray: 执行成功");
        String mJson = json.toString();
        Gson gson = new Gson();
        list = gson.fromJson(mJson,new TypeToken<ArrayList<AppNameBean>>(){}.getType());
        Log.i("777", "parseArray: size"+list.size());
//        for (int i = 0; i < list.size(); i++) {
//            productName = list.get(i).getProductName();
//            id = list.get(i).getId();
//            appacount判断是否有重复
            DBHelper dbHelper = DBHelper.getInstance(loginId);
            SQLiteDatabase sql=dbHelper.getWritableDatabase();
            Cursor appAcountCursor=sql.rawQuery("select appname from appacount",null);
            while (appAcountCursor.moveToNext()){
                appName = appAcountCursor.getString(appAcountCursor.getColumnIndex("appname"));

            }
            appAcountCursor.close();
            if (appName == null){
                insertDBappacount();
            }
        }

//    }

    /**
     * 向appacount表插入数据
     */
    private void insertDBappacount() {
        try {
            DBHelper dbHelper = DBHelper.getInstance(loginId);
            SQLiteDatabase db = dbHelper.getWritableDatabase();
            dbHelper.getWritableDatabase();
            ContentValues values = new ContentValues();
            for (int i = 0; i < list.size(); i++) {
                id = list.get(i).getId();
                productName = list.get(i).getProductName();
                values.put("appid", id);
                values.put("appname", productName);
                db.insert("appacount", null, values);
                values.clear();
            }
        } catch (Exception e) {
            System.out.println("不可重复插入");
        }

    }

    public void getAppName() {
        Call<ArrayInfo> appNameArray = responseInfoApi.appName();
        appNameArray.enqueue(new CallBackArrayAdapter());
    }
}
